package com.topc.platform.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.platform.model.po.AnnouncementInfo;
import com.topc.platform.model.vo.AnnouncementShowVo;
import com.topc.platform.service.IAnnouncementInfoService;
import com.topc.platform.model.dto.SaveAnnouncementDto;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/close/announcement")
public class AnnouncementCloseController {

    @Autowired
    private IAnnouncementInfoService announcementInfoService;

    @PostMapping("/search-info")
    public CommonResult<PageResult<AnnouncementShowVo>> searchInfoByPage(PageParams pageParams, String title, @Range(min = -2, max = 2) int status) {
        PageResult<AnnouncementShowVo> announcementInfoPageResult = announcementInfoService.searchInfoByPage(pageParams, title, status);
        return CommonResult.success(announcementInfoPageResult);
    }

    @GetMapping("/get-detail-by-id")
    public CommonResult<AnnouncementShowVo> getDetailById(@NotNull Long id) {
        AnnouncementShowVo announcementShowVo = announcementInfoService.getDetailById(id);
        return CommonResult.success(announcementShowVo);
    }

    @GetMapping("/get-info-by-id")
    public CommonResult<AnnouncementInfo> getInfoById(@NotNull Long id) {
        AnnouncementInfo announcementInfo = announcementInfoService.getInfoById(id);
        return CommonResult.success(announcementInfo);
    }

    @PostMapping("/save")
    public CommonResult<String> saveInfo(@RequestBody @Validated SaveAnnouncementDto saveAnnouncementDto) {
        announcementInfoService.saveInfo(saveAnnouncementDto);
        return CommonResult.success("添加成功");
    }

    @PostMapping("/update")
    public CommonResult<String> updateInfoById(@RequestBody @Validated SaveAnnouncementDto saveAnnouncementDto, @NotNull Long id) {
        announcementInfoService.updateInfoById(id, saveAnnouncementDto);
        return CommonResult.success("修改成功");
    }

    @GetMapping("/delete")
    public CommonResult<String> deleteInfoById(@NotNull Long id) {
        announcementInfoService.removeInfoById(id);
        return CommonResult.success("删除成功");
    }
}
